<?php
/**
 * 景点
 */
namespace App\Http\Controllers\Admin\Scene;

use App\Area;
use App\Device;
use App\Http\Controllers\Admin\Scene\Service\Spot;
use App\Http\Controllers\Admin\Scene\Repository\Spot as SpotRepository;
use App\Repositories\PageRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SpotController extends Controller
{
    private $manager;
    private $repository;
    private $request;
    private $viewPrefix = 'admin.scene.spot.';
    private $routePrefix = 'admin.scene.spot';


    public function __construct (Request $request)
    {
        $this->manager = new Spot();
        $this->repository = new SpotRepository();
        $this->request = $request;
        $this->pageRepositiory=new PageRepository();

    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{

            return view($this->viewPrefix.'index',[
                'status' =>    $this->manager->getStatusList(),
                'manager'   =>  $this->manager,
                'routePrefix'   =>  $this->routePrefix
            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){
        return $this->page_json_render($this->manager->getPageList($this->request));
    }


    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $model = $this->manager->getSpot($id);

        return view($this->viewPrefix.'add-edit',[
            'status' =>    $this->manager->getStatusList(),
            'areas' =>  Area::all()->toArray(),
            'manager'   =>  $this->manager,
            'id'    =>  $id,
            'model' =>  $model,
            'routePrefix'   =>  $this->routePrefix

        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        return view($this->viewPrefix.'add-edit',[
            'status' =>    $this->manager->getStatusList(),
            'areas' =>  Area::all()->toArray(),
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(){
        //dd($this->request->post());
        $this->validate($this->request,[
            'title'    =>  'required|max:50',
            'desc'  =>  'required|max:200',
            'voice'    =>  'required',
            'thumb' =>  'required'
        ]);
        $this->manager->insertOrUpdate($this->request->post());
        return redirect()->route($this->routePrefix)->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function draft(){
            $this->manager->draft($this->request->id);
            return  ['info'  =>  'success','msg'    =>  $this->request->id];
    }

    /**
     * @return array
     */
    private function published(){
            $this->manager->published($this->request->id);
            return  ['info'  =>  'success'];
    }

    /**
     * @return array
     */
    private function delete(){
            $model = new Device();
            $info = $this->manager->getSpot($this->request->id);
            if(!$info){
                return ['info'=>'fail','msg'=>'该景点已删除'];
            }
            if($info->page_id){
                $res = $model->where('page_id',$info->page_id)->first();
            }else{
                $res = false;
            }
            if($res){
                return ['info'=>'fail','msg'=>'该景点绑有设备，请先解绑再进行删除'];
            }else{
                $this->manager->del($this->request->id);
                return  ['info'  =>  'success'];
            }

    }

    private function sortCol(){
            $this->manager->sortCol($this->request->id,$this->request->sort);
            return  ['info'  =>  'success'];
    }
}
